﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：改户
//********************************************************************************//
using System;
using Entity;
using BLL;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.YongHuGL
{
    public partial class GaiHu : BasePage,Web.UC.IConfirmDo
    {
        #region 属性
        
        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int YONGHUXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_YONGHUXXID"]);
            }
            set
            {
                ViewState["_YONGHUXXID"] = value;
            }
        }

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }
       
        #endregion

        #region 变量
        
        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();                
                Display();                
            }
            this.UCConfirm1.ConfirmDoPage = this;
        }

        /// <summary>
        /// 添加操作
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            InitData();
            this.Status = GridViewStatus.Add;
            Display();
            this.upEdit.Update();
        }

        /// <summary>
        /// 放弃操作
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
            this.upEdit.Update();
        }
      
        /// <summary>
        /// 查询操作
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.txtLaiHanBH.Text.Trim() == "")
            {
                return;
            }
            this.Status = GridViewStatus.Search;
            //根据来涵编号查询出用户号,暂写固定
            string yonghuH = "0100040";
            this.txtYongHuH.Text = yonghuH;
            //查询下一月用户信息
            DataTable dt = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}' and I_LEIXING=1 and I_JLZT=0", yonghuH), null);
            if (dt.Rows.Count != 0)
            {
                this.YONGHUXXID = Convert.ToInt32(dt.Rows[0]["ID"]);
                SetData();
                Display();
                this.upEdit.Update();
            }
            else
            {
                PageTools.AlertMsg(this, "该用户号无可修改的数据");
            }
        }
       
        /// <summary>
        /// 保存操作
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
            this.upEdit.Update();
        }

        /// <summary>
        /// 局下拉框选中项变更事件
        /// </summary>
        protected void cmbJu_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGongSiByJu();
        }
     
        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANMC", "ZHANDIANBH", null, string.Format("USERID={0}",LoginInfo.CurrentUser.UserID)).Tables[0];
            string zhandianBH = dtZhanDian.Rows[0]["VALUE"].ToString();
            string zhandianMC = dtZhanDian.Rows[0]["TEXT"].ToString();
            //加载行政区
            PageTools.BindComboBox(
                this.cmbXingZhengQ, BusinessTool.GetTextAndValue(
                    "YS_JX_XINGZHENGQ", "S_MINGCHENG", "S_BIANHAO", null, null
                ).Tables[0]
            );
            this.cmbXingZhengQ.SelectedIndex = 0;
            //操作员站点为本部时 加载局信息
            if (zhandianMC.Contains("本部"))
            {
                this.cmbJu.ReadOnly = false;
                PageTools.BindComboBox(
                    this.cmbJu, BusinessTool.GetTextAndValue(
                        "YS_JX_JUXINX", "S_MINGCHENG", "S_BIANHAO", null, null
                    ).Tables[0]
                );
                if (this.cmbJu.Items.Count > 0)
                {
                    this.cmbJu.SelectedIndex = 0;
                    this.LoadGongSiByJu();
                }
            }
            else
            {
                this.cmbJu.ReadOnly = true;
                this.cmbGongSi.ReadOnly = true;
            }
            //加载业务员
            PageTools.BindComboBox(
                this.cmbYeWuY, BusinessTool.GetTextAndValue(
                "View_AS_UserInfo", "USERNAME", "USERID", "-", zhandianMC.Contains("本部") ? string.Format(" ROLENAME like '%业务员%'") : string.Format("ZHANDIANBH='{0}' and ROLENAME like '%业务员%'", zhandianBH)
                ).Tables[0]
            );
            if (this.cmbYeWuY.Items.Count != 0)
            {
                this.cmbYeWuY.SelectedIndex = 0;
            }
            //加载用水性质
            PageTools.BindComboBox(
                 this.cmbYongShuiXZ, BusinessTool.GetTextAndValue(
                 "YS_JX_YONGSHUIXZ", "S_MINGCHENG", "S_BIANMA", "-", string.Format("I_TYPE={0}", zhandianMC.Contains("本部") ? 1 : 0)
                 ).Tables[0]
             );
            if (this.cmbYongShuiXZ.Items.Count != 0)
            {
                this.cmbYongShuiXZ.SelectedIndex = 0;
            }
            //加载考核复用
            WordsManager manger = new WordsManager();
            PageTools.BindComboBox(this.cmbKaoHeFY, manger.Instance.FindWordsDataTableWithMiddleAffixByBelongID(WordsStruct.BelongID.考核复用,"-"));
            if (this.cmbKaoHeFY.Items.Count != 0)
            {
                this.cmbKaoHeFY.SelectedIndex = 0;
            } 
            //加载行业类型
            PageTools.BindComboBox(
                 this.cmbSheShuiDXDM, BusinessTool.GetTextAndValue(
                 "YS_JX_HANGYELX", "S_SHESHUIDXFLMC", "S_SHESHUIDXFLDM", null, null
                 ).Tables[0]
             );
            if (this.cmbSheShuiDXDM.Items.Count != 0)
            {
                this.cmbSheShuiDXDM.SelectedIndex = 0;
            }            
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtKaiHuHM.Text = "";
            this.txtKaiHuYH.Text = "";
            this.txtLianXiBM.Text = "";
            this.txtLianXiDH.Text = "";
            this.txtLianXiR.Text = "";
            this.txtNewYongHuH.Text = "";
            this.txtYHZH.Text = "";
            this.txtYongHuDZ.Text = "";
            this.txtYONGHUM.Text = "";
            this.txtYouBian.Text = "";
            this.txtYSJ.Text = "";
            this.cmbGongSi.Text = "";
            this.cmbJu.Text = "";
            this.cmbKaoHeFY.Text = "";
            this.cmbSheShuiDXDM.Text = "";
            this.cmbXingZhengQ.Text = "";
            this.cmbYeWuY.Text = "";
            this.cmbYongShuiXZ.Text = "";
            this.cbTuoShou.Checked = false;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {  
     
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    InitData();
                    SetText(false);                    
                    break;
                case GridViewStatus.Search:
                    SetData();
                    SetText(true);
                    this.txtNewYongHuH.Focus();
                    break;
                case GridViewStatus.Update:                  
                    break;
                case GridViewStatus.Add:                    
                    break;
            }
        }

        /// <summary>
        /// 设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            foreach (var item in this.panEdit.Controls)
            {
                DevExpress.Web.ASPxWebControl control = item as DevExpress.Web.ASPxWebControl;
                if (control != null)
                {
                    control.Enabled = b;
                }
            }
        }      

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            //获取当前操作用户信息
            this._YS_YG_YONGHUXX = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXX(this.YONGHUXXID);
            this.txtKaiHuHM.Text = this._YS_YG_YONGHUXX.S_KAIHUHM;
            this.txtKaiHuYH.Text = this._YS_YG_YONGHUXX.S_KAIHUYH;
            this.txtLianXiBM.Text = this._YS_YG_YONGHUXX.S_LIANXIBM;
            this.txtLianXiDH.Text = this._YS_YG_YONGHUXX.S_LIANXIDH;
            this.txtLianXiR.Text = this._YS_YG_YONGHUXX.S_LIANXIR;
            this.txtNewYongHuH.Text = this._YS_YG_YONGHUXX.S_YONGHUH;
            this.txtYHZH.Text = this._YS_YG_YONGHUXX.S_YINHANGZH;
            this.txtYongHuDZ.Text = this._YS_YG_YONGHUXX.S_DIZHI;
            this.txtYONGHUM.Text = this._YS_YG_YONGHUXX.S_YONGHUM;
            this.txtYouBian.Text = this._YS_YG_YONGHUXX.S_YOUBIAN;
            this.txtYSJ.Text = this._YS_YG_YONGHUXX.N_YSJ.ToString();
            this.cmbJu.Value = this._YS_YG_YONGHUXX.S_JUBIANH;
            //更新了选中局,重新加载一下公司
            LoadGongSiByJu();
            if(this.cmbGongSi.Items.Count!=0)
            {
                this.cmbGongSi.Value = this._YS_YG_YONGHUXX.S_GONGSIBH;
            }
            this.cmbKaoHeFY.Value = this._YS_YG_YONGHUXX.I_KAOHEFY.ToString();
            this.cmbSheShuiDXDM.Value = this._YS_YG_YONGHUXX.S_SHESHUIDXDM;
            this.cmbXingZhengQ.Value = this._YS_YG_YONGHUXX.S_XINGZHENGQBH;
            this.cmbYeWuY.Value = this._YS_YG_YONGHUXX.S_YEWUY;
            this.cmbYongShuiXZ.Value = this._YS_YG_YONGHUXX.S_YONGSHUIXZ;
            this.cbTuoShou.Checked = this._YS_YG_YONGHUXX.I_SHOUFEIFS == 0;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //当前无操作的数据则返回
            if (this.Status==GridViewStatus.Init)
            {
                return;
            }
            //修改操作
            operateState = OperateState.Modify;
            //在点查询按钮时已经获取的即是下月数据的ID
            this._YS_YG_YONGHUXX = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXX(this.YONGHUXXID);
            
            //判断用户号合法性
            if (this.txtNewYongHuH.Text.Substring(0, 2) != this._YS_YG_YONGHUXX.S_GUANLIKBH)
            {
                PageTools.AlertMsg(this, "该用户号不属于本管理块,请重新填写用户号 !");
                return;
            }
            //判断用户号是否重复
            if (_YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}' and I_JLZT=0 and I_LeiXing=1 and ID<>{1}", this.txtNewYongHuH.Text, _YS_YG_YONGHUXX.ID), null).Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "该用户号系统中已有使用,请更换!");
                return;
            }
            this._YS_YG_YONGHUXX.I_KAOHEFY = Convert.ToDecimal(this.cmbKaoHeFY.Value);
            this._YS_YG_YONGHUXX.I_YONGHUZT = 0;
            this._YS_YG_YONGHUXX.N_YSJ = Convert.ToDouble(this.txtYSJ.Text);
            this._YS_YG_YONGHUXX.S_DIZHI = this.txtYongHuDZ.Text;
            if (this.cmbGongSi.SelectedIndex != -1)
            {
                this._YS_YG_YONGHUXX.S_GONGSIBH = Convert.ToString(this.cmbGongSi.Value);
            }
            if (this.cmbJu.SelectedIndex != -1)
            {
                this._YS_YG_YONGHUXX.S_JUBIANH = Convert.ToString(this.cmbJu.Value);
            }
            this._YS_YG_YONGHUXX.S_KAIHUHM = this.txtKaiHuHM.Text;
            this._YS_YG_YONGHUXX.S_KAIHUYH = this.txtKaiHuYH.Text;
            this._YS_YG_YONGHUXX.S_LIANXIBM = this.txtLianXiBM.Text;
            this._YS_YG_YONGHUXX.S_LIANXIDH = this.txtLianXiDH.Text;
            this._YS_YG_YONGHUXX.S_LIANXIR = this.txtLianXiR.Text;
            this._YS_YG_YONGHUXX.S_SHESHUIDXDM = this.cmbSheShuiDXDM.Value.ToString();
            this._YS_YG_YONGHUXX.S_XINGZHENGQBH = this.cmbXingZhengQ.Value.ToString();
            this._YS_YG_YONGHUXX.S_YEWUY = this.cmbYeWuY.Value.ToString();
            this._YS_YG_YONGHUXX.S_YINHANGZH = this.txtYHZH.Text;
            this._YS_YG_YONGHUXX.S_YONGHUH = this.txtNewYongHuH.Text;
            this._YS_YG_YONGHUXX.S_YONGHUM = this.txtYONGHUM.Text;
            this._YS_YG_YONGHUXX.S_YONGSHUIXZ = this.cmbYongShuiXZ.Value.ToString();
            this._YS_YG_YONGHUXX.S_YOUBIAN = this.txtYouBian.Text;
            this._YS_YG_YONGHUXX.I_SHOUFEIFS = this.cbTuoShou.Checked ? 0 : 1;
            this._YS_YG_YONGHUXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_YG_YONGHUXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            //其他信息
            _SYS_LOGBLL.Log<YS_YG_YONGHUXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-户籍管理-开户", operateState, _YS_YG_YONGHUXX, null);
            if (_YS_YG_YONGHUXXBLL.InsertOrUpdate(_YS_YG_YONGHUXX, null))
            {
                

                this.UCAlert1.Msg = "保存成功！";
                this.UCAlert1.Show();
                this.Status = GridViewStatus.Search;
                Display();
                this.upEdit.Update();
            }
            else
            {
               this.UCAlert1.Msg="保存失败";
                this.UCAlert1.Show();
            }
        }

        /// <summary>
        /// 根据局重新加载对应的公司
        /// </summary>
        private void LoadGongSiByJu()
        {
            DataTable table = BusinessTool.GetTextAndValue(
                       "YS_JX_GONGSIXX", "S_MINGCHENG", "S_BIANHAO", null, string.Format("S_JUBIANH='{0}'", this.cmbJu.Value)
                   ).Tables[0];

            if (table.Rows.Count > 0)
            {
                PageTools.BindComboBox(this.cmbGongSi, table);
                this.cmbGongSi.SelectedIndex = 0;
            }
            else
            {
                this.cmbGongSi.Items.Clear();
                this.cmbGongSi.Text = "";
                this.cmbGongSi.Value = "";
            }
        }

        /// <summary>
        ///下拉框选中变化时变更指定的文本框值 
        /// </summary>
        /// <param name="textbox">要赋值的文本框</param>
        private void CmbValueToTxtText(object sender, EventArgs e, DevExpress.Web.ASPxTextBox textbox)
        {
            DevExpress.Web.ASPxComboBox cmb = sender as DevExpress.Web.ASPxComboBox;
            if (cmb.SelectedIndex != -1)
            {
                textbox.Text = cmb.Value.ToString();
            }
        }

        /// <summary>
        /// 实现confirm接口的确定按钮事件
        /// </summary>
        public void ConfirmDo()
        {
            throw new NotImplementedException();
        }
        #endregion

       
    }
}